package cn.udrm.water.base.v1.manager.repository;

import java.util.List;

import javax.validation.constraints.NotNull;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.EntityGraph.EntityGraphType;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import cn.udrm.water.base.v1.manager.entities.BaseDepartment;

@Repository
public interface BaseDepartmentRepository extends JpaRepository<BaseDepartment, String>
		, JpaSpecificationExecutor<BaseDepartment>{
	
	public List<BaseDepartment> findByParentId(String parentId);
	
	@EntityGraph(value="BaseDepartment.childrens", type=EntityGraphType.FETCH)
	@Query(value="FROM BaseDepartment w WHERE w.id=?1")
	public BaseDepartment getWithChildrens(String id);
	
	@EntityGraph(value="BaseDepartment.rolesAndParentAndOrg", type=EntityGraphType.FETCH)
	@Query(value="FROM BaseDepartment w WHERE w.id=?1")
	public BaseDepartment getWithRoles(String id);
	
	@EntityGraph(value="BaseDepartment.persons", type=EntityGraphType.FETCH)
	@Query(value="FROM BaseDepartment w WHERE w.id=?1")
	public BaseDepartment getWithPersons(String id);
	
	@EntityGraph(value="BaseDepartment.childrens", type=EntityGraphType.FETCH)
	public List<BaseDepartment> findByOrganizationId(String organizationId);

	public Page<BaseDepartment> findAll(@NotNull Specification<BaseDepartment> specification, Pageable pageable);
	
	@Query(value="FROM BaseDepartment w WHERE w.id=?1")
	public BaseDepartment findSelfById(String id);
	
}
